package com.dcjt.firehotcarpad.helper

import android.annotation.SuppressLint
import android.content.Context
import android.text.Html
import android.text.Spanned
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import androidx.appcompat.app.AlertDialog
import com.aries.library.fast.manager.LoggerManager
import com.aries.library.fast.util.SizeUtil
import com.dcjt.firehotcarpad.R
import com.dcjt.firehotcarpad.entity.InstructionListEntity
import com.dcjt.firehotcarpad.utils.StringUtils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.dialog_countdown_layout.view.*
import kotlinx.android.synthetic.main.dialog_countdown_layout.view.dialog_cancel
import kotlinx.android.synthetic.main.dialog_countdown_layout.view.dialog_confirm
import kotlinx.android.synthetic.main.dialog_countdown_layout.view.dialog_title
import kotlinx.android.synthetic.main.dialog_instruction_countdown_layout.view.*
import java.util.concurrent.TimeUnit

/**
 * Created by cj on 2021/2/20.
 * Email:codesexy@163.com
 * Function:
 * desc:
 */
object CountDownDialogHelper{

    /**
     * 开启倒计时弹窗
     * @param title 标题
     * @param content 内容
     * @param confirmText 确认按钮文字
     * @param callback 确认按钮事件回调
     * @param countDown 倒计时时间 默认为10秒
     */

    @SuppressLint("SetTextI18n")
    fun showCountDownDialog(
        context:Context,
        title: String,
        content: String,
        confirmText: String,
        callback: (() -> Unit),
        cancelCallback: (() -> Unit),
        countDown: Int = 20
    ): AlertDialog {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_countdown_layout, null, false)
        val dialog = AlertDialog.Builder(context).setCancelable(false).setView(view).create()
        view.dialog_title.text = title
        view.dialog_content.text = content
        view.dialog_confirm.text = "${confirmText}($countDown)"
        val d = getTimer {
            if (it == countDown) {
                view.dialog_confirm.performClick()
            }
            view.dialog_confirm.text = "${confirmText}(${countDown - it})"
        }
        view.dialog_cancel.setOnClickListener {
            d.dispose()
            dialog.dismiss()
        }
        view.dialog_confirm.setOnClickListener {
            d.dispose()
            dialog.dismiss()
            callback()
        }
        dialog.show()
        //此处设置位置窗体大小，我这里设置为了手机屏幕宽度的3/4  注意一定要在show方法调用后再写设置窗口大小的代码，否则不起效果会

        dialog.window?.setLayout(
            (SizeUtil.getScreenWidth() / 2),
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        return dialog
    }

    /**
     * 开启指令倒计时弹窗
     * @param title 标题
     * @param content 内容
     * @param confirmText 确认按钮文字
     * @param callback 确认按钮事件回调
     * @param countDown 倒计时时间 默认为10秒
     */

    @SuppressLint("SetTextI18n")
    fun showInstructionCountDownDialog(
        context:Context,
        entity: InstructionListEntity,
        confirmText: String,
        callback: (() -> Unit),
        cancelCallback: (() -> Unit),
        countDown: Int = 15
    ): AlertDialog {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_instruction_countdown_layout, null, false)
        val dialog = AlertDialog.Builder(context).setCancelable(false).setView(view).create()
        view.dialog_title.text =if (!StringUtils.isEmpty(entity.zlmc)) "${entity!!.zlmc}" else "无主题"
//        view.dialog_fsr.text = getStringSpanned("发送单位：","${entity.fsdwmc}_${entity.fsrmc}")
        view.dialog_fsr.text = "${entity.fsdwmc}_${entity.fsrmc}"
        view.dialog_fj.text = (if (!StringUtils.isEmpty(entity!!.fssj as String?)) entity!!.fssj else "未记录").toString()
//        view.dialog_fj.text = getStringSpanned(" ",
//            (if (!StringUtils.isEmpty(entity!!.fssj as String?)) entity!!.fssj else " ") as String
//        )
//        view.dialog_nr.text =getStringSpanned("内容： ","${entity.zlnr}")
        view.dialog_nr.text ="${entity.zlnr}"


        if (!StringUtils.isEmpty(confirmText)){
            view.dialog_confirm.text = "${confirmText}($countDown)"
        } else{
            view.dialog_confirm.visibility = View.GONE
        }
        val d = getTimer {
            LoggerManager.e("倒计时时间 $it")
            if (it == countDown) {
                view.dialog_confirm.performClick()
            }
            view.dialog_confirm.text = "${confirmText}(${countDown - it})"
        }
        view.dialog_cancel.setOnClickListener {
            d.dispose()
            dialog.dismiss()
        }
        view.dialog_confirm.setOnClickListener {
            d.dispose()
            dialog.dismiss()
            callback()
        }
        dialog.show()
        //此处设置位置窗体大小，我这里设置为了手机屏幕宽度的3/4  注意一定要在show方法调用后再写设置窗口大小的代码，否则不起效果会
        dialog.window?.setLayout(
            (SizeUtil.getScreenWidth() *3/4),
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        return dialog
    }

    fun getStringSpanned(title:String,str:String) : Spanned {
       return Html.fromHtml(String.format("<font color=\"#999999\"><b>%s</b></font><font color=\"#333333\">%s</font>",title,str))
    }
    private fun getTimer(callback: (Int) -> Unit): Disposable {
        var count = 0
        return Observable.interval(1000, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                count++
                callback(count)
            }
    }



}